package ua.org.enginer.managers
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	import ua.org.enginer.remoting.events.RemoteExceptionEvent;
	import ua.org.enginer.remoting.events.RemoteResultEvent;
	import ua.org.enginer.remoting.services.RemoteFileService;
	
	[Event(name='complete', type='ua.org.enginer.remoting.events.RemoteResultEvent')]
	
	public class UploadManager extends EventDispatcher {
		/*
		private static var _instance:UploadManager;
		public static function get instance():UploadManager {
			if (!_instance) {
				_instance = new UploadManager();
			}
			
			return _instance;
		}
		*/
		public static const TYPE_RESTRICTION:Array = [".php", ".phtml", ".php3", ".php4"]
		public static const SIZE_RESTRICTION:uint = 10485760 //10 MB
		
		public var fileReference:FileReference;
		private var service:RemoteFileService;
		private var fileData:ByteArray

		public var useBusyCursor:Boolean = true
		public var autoUpload:Boolean
		public var path:String
		//public var fileName:String
		
		public function browse(path:String='', autoUpload:Boolean = true):void {
	    	 
	    	 this.path = path
	    	 this.autoUpload = autoUpload
	    	 
	    	 fileReference = new  FileReference(); 
             fileReference.browse(); 
             fileReference.addEventListener(Event.SELECT, onFileSelect);
             fileReference.addEventListener(Event.COMPLETE, onFileComplete);
		}
		
		private function onFileSelect(event:Event):void {
			//fileName = fileReference.name
			fileReference.load()
		}

		private function onFileComplete(event:Event):void {
			if (TYPE_RESTRICTION.indexOf(fileReference.type) >= 0) {
				Alert.show("Запрещённый тип файла", "Отмена загрузки")
				return
			}
			if (fileReference.size > SIZE_RESTRICTION) {
				Alert.show("Привышение допустимого размера файла (10Мб)", "Отмена загрузки")
				return
			}
			
			
			fileData = new ByteArray()
			fileReference.data.readBytes(fileData, 0, fileReference.data.length);
			if (autoUpload) upload()
		}
		
		protected function upload():void {
			//file = new FileVO();
			//file.filename = fileReference.name;
			//file.filedata = fileData;
			service = new RemoteFileService()
			service.addEventListener(RemoteExceptionEvent.REMOTE_EXCEPTION, handleRemoteExceptionEvent);
			//service.addEventListener(RemoteResultEvent.ERROR, handleRemoteExceptionEvent);
			service.addEventListener(RemoteResultEvent.COMPLETE, handleRemoteResultEvent);
			
			//var type:String = fileReference.name
			//type = type.slice(type.lastIndexOf(".")+1)
			
			service.upload(fileData, path, fileReference.type);
			if (useBusyCursor) CursorManager.setBusyCursor()
		} 
		
		private function handleRemoteResultEvent(event:RemoteResultEvent):void {
			//Alert.show(event.message);
			if (useBusyCursor) CursorManager.removeBusyCursor()
			dispatchEvent(event)
		}
		
		private function handleRemoteExceptionEvent(event:RemoteExceptionEvent):void {
		    if (useBusyCursor) CursorManager.removeBusyCursor()
		    Alert.show(event.message);
		}
	}
}